﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DrawTools;
using System.Drawing;
using System.Drawing.Drawing2D;
using KHMI.Attributes;
using System.Windows.Forms;

namespace DrawTools
{
    public class DrawWatch 
        : DrawRectangle, IDisposable
    {
        const double PEI = 3.1415926;

        private int mV;
        private int mVV;
        private int mMaxMain;
        private int mMaxM;

        [ExtendCategoryAttribute("仪表设置", VectorName = "当前刻度1")]
        public int V//当前刻度１
        {
            get
            {
                return mV;
            }
            set
            {
                if (mV == value) return;
                mV = value;
            }
        }

        [ExtendCategoryAttribute("仪表设置", VectorName = "当前刻度2")]
        public int VV//当前刻度２
        {
            get
            {
                return mVV;
            }
            set
            {
                if (mVV == value) return;
                mVV = value;
            }
        }

        [ExtendCategoryAttribute("仪表设置", VectorName = "最大刻度")]
        public int MaxMain//最大刻度
        {
            get
            {
                return mMaxMain;
            }
            set
            {
                if (mMaxMain == value) return;
                mMaxMain = value;
            }
        }

        [ExtendCategoryAttribute("仪表设置", VectorName = "最大刻度2")]
        public int MaxM//ＭａｘＭ（刻度二的最大值）
        {
            get
            {
                return mMaxM;
            }
            set
            {
                if (mMaxM == value) return;
                mMaxM = value;
            }
        }

        public DrawWatch() : this(0, 0, 1, 1)
		{
            IsCusControl = true;
		}

        public DrawWatch(int x, int y, int width, int height)
            : base()
        {
            IsCusControl = true;
            Rectangle = new Rectangle(x, y, width, height);
            Initialize();
        }

        /// <summary>
        /// Clone this instance
        /// </summary>
        public override DrawObject Clone(DrawObject drawObject)
        {
            base.Clone(drawObject);
            return this;
        }

        public override void SaveToStream(System.Runtime.Serialization.SerializationInfo info, int orderNumber)
        {
            //info.AddValue("cube" + orderNumber.ToString(), Cube);

            base.SaveToStream(info, orderNumber);
        }

        public override void LoadFromStream(System.Runtime.Serialization.SerializationInfo info, int orderNumber)
        {
            //Cube = info.GetInt32("cube" + orderNumber.ToString());

            base.LoadFromStream(info, orderNumber);
        }

        public override void Draw(Graphics graphics, ref KKDraw drawArea)
        {
            Rectangle rect1 = DrawRectangle.GetNormalizedRectangle(Rectangle);

            Control[] cls = drawArea.Controls.Find(this.ID.ToString(), false);

            if (cls.Length > 0)
            {
                CusControl = (Button)cls[0];
                CusControl.Location = rect1.Location;
                CusControl.Size = rect1.Size;
            }
            else
            {
                CusControl = new Button();
                CusControl.Name = this.ID.ToString();
                CusControl.Text = "测试咯";
                CusControl.Location = rect1.Location;
                CusControl.Size = rect1.Size;
                drawArea.Controls.Add(CusControl);
            }

            if (IsView == false)
            {
                CusControl.Enabled = false;
            }
        }

        /// <summary>
        /// Draw Watch
        /// </summary>
        /// <param name="g"></param>
        public override void Draw(Graphics graphics)
        {
            try
            {
                Rectangle rect1 = DrawRectangle.GetNormalizedRectangle(Rectangle);
                rect1.Y += 3;
                rect1.Height = rect1.Width;
                graphics.DrawArc(Pens.DarkGray, rect1, 180 + 30, 120);//画表盘圆弧１

                Rectangle rect2 = DrawRectangle.GetNormalizedRectangle(Rectangle);
                rect2.Y += 3;
                rect2.X += 15;
                rect2.Y += 15;
                rect2.Width -= 30;
                rect2.Height = rect2.Width;
                graphics.DrawArc(Pens.DarkGray, rect2, 180 + 30, 120);//画表盘圆弧２
                Rectangle rect3 = rect2;
                rect3.X += 50;
                rect3.Y += 50;
                rect3.Width -= 100;
                rect3.Height -= 100;
                graphics.DrawArc(Pens.DarkGray, rect3, 180 + 30, 120);//画表盘圆弧３


                Rectangle rect4 = rect2;
                rect4.X += 10;
                rect4.Y += 10;
                rect4.Width -= 20;
                rect4.Height -= 20;
                graphics.DrawArc(Pens.DarkGray, rect4, 180 + 30, 120);//画表盘圆弧４

                int ox = rect1.Width / 2 + rect1.Left;
                int oy = rect1.Height / 2 + rect1.Top;

                double rad = 0;
                Point pt1 = new Point(0, 0);
                Point pt2 = new Point(0, 0);

                for (int i = 0; i < 120 / 10; i++)//画大刻度
                {
                    double _rad = PEI / 180 * 10;
                    rad = (_rad * i + (PEI / 180 * 30));
                    pt1 = Trans(rect1.Width / 2, rad, ox, oy);
                    pt2 = Trans(rect2.Width / 2, rad, ox, oy);
                    graphics.DrawLine(Pens.DarkGray, pt1, pt2);
                    pt1 = Trans(rect2.Width / 2, rad, ox, oy);
                    pt2 = Trans(rect4.Width / 2, rad, ox, oy);
                    graphics.DrawLine(Pens.DarkGray, pt1, pt2);

                }
                for (int i = 0; i < 120 / 2; i++)//画细刻度
                {
                    double _rad = PEI / 180 * 2;
                    rad = (_rad * i + (PEI / 180 * 30));
                    pt1 = Trans(rect1.Width / 2, rad, ox, oy);
                    pt2 = Trans(rect1.Width / 2 - 5, rad, ox, oy);
                    graphics.DrawLine(Pens.DarkGray, pt1, pt2);
                    pt1 = Trans(rect2.Width / 2, rad, ox, oy);
                    pt2 = Trans(rect2.Width / 2 - 5, rad, ox, oy);
                    graphics.DrawLine(Pens.DarkGray, pt1, pt2);
                }
                pt1 = Trans(rect1.Width / 2, PEI / 180 * 30, ox, oy);
                pt2 = Trans(rect3.Width / 2, PEI / 180 * 30, ox, oy);
                graphics.DrawLine(Pens.DarkGray, pt1, pt2);//画表盘直边１
                pt1 = Trans(rect1.Width / 2, PEI / 180 * 150, ox, oy);
                pt2 = Trans(rect3.Width / 2, PEI / 180 * 150, ox, oy);
                graphics.DrawLine(Pens.DarkGray, pt1, pt2);//画表盘直边２
                double v = 120;
                v /= MaxMain;
                v *= V;
                v = (PEI / 180 * v + PEI / 180 * 30);
                pt1 = Trans(rect1.Width / 2, v, ox, oy);
                pt2 = Trans(rect2.Width / 2, v, ox, oy);
                graphics.DrawLine(Pens.Red, pt1, pt2);//画指针１


                v = 120;
                v /= MaxMain;
                v *= VV;
                v = (PEI / 180 * v + PEI / 180 * 30);
                pt1 = Trans(rect2.Width / 2, v, ox, oy);
                pt2 = Trans(rect4.Width / 2, v, ox, oy);
                graphics.DrawLine(Pens.Red, pt1, pt2);//画指针２

                pt1 = Trans(rect3.Width / 2 - 5, PEI / 180 * 50, ox, oy);
                pt2 = Trans(rect3.Width / 2 - 5, PEI / 180 * 130, ox, oy);
                Rectangle tr = new Rectangle(pt2, new Size(pt1.X - pt2.X, 15));
                FontFamily fontFamily = new FontFamily("Arial");
                Font font = new Font(
                 fontFamily,
                 9,
                 FontStyle.Regular,
                 GraphicsUnit.Point);
                graphics.DrawRectangle(Pens.DarkBlue, tr);
                graphics.DrawString(V.ToString() + "." + VV.ToString(), font, Brushes.Red, tr);//绘制数字窗口
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            

















            //Rectangle clientrect = DrawRectangle.GetNormalizedRectangle(Rectangle);

            ////设置路码表的高和宽
            //float WIDTH = clientrect.X + clientrect.Width;
            //float HEIGHT = clientrect.Y + clientrect.Height;

            //FontFamily fontFamily = new FontFamily("Times New Roman");
            //Font font = new Font(fontFamily, 14, FontStyle.Bold, GraphicsUnit.Pixel);

            ////将绘图平面的坐标原点移到窗口中心
            //graphics.TranslateTransform(WIDTH / 2, HEIGHT / 2);
            ////绘制仪表盘
            //graphics.FillEllipse(Brushes.Black,
            //    HEIGHT / -2, HEIGHT / -2, HEIGHT, HEIGHT);
            ////输出文本
            //graphics.DrawString("公里/小时", font, Brushes.Green,
            //    new PointF(-26, HEIGHT / -4 - font.Height));

            ////绘制刻度标记
            //graphics.RotateTransform(225);
            //for (int x = 0; x < 55; x++)
            //{
            //    graphics.FillRectangle(Brushes.Green,
            //        -2, (HEIGHT / 2 - 2) * -1, 3, 15);
            //    graphics.RotateTransform(5);
            //}

            ////重置绘图平面的坐标变换
            //graphics.ResetTransform();

            //graphics.TranslateTransform(WIDTH / 2, HEIGHT / 2);
            //graphics.RotateTransform(225);
            //int sp = 0;
            //string tmp = string.Empty;
            ////绘制刻度值(整数类)
            //for (int x = 0; x < 7; x++)
            //{
            //    tmp = string.Format("{0}", sp);

            //    //绘制红色刻度
            //    graphics.FillRectangle(Brushes.Red, -3, (HEIGHT / 2 - 2) * -1, 6, 25);
            //    //绘制数值
            //    graphics.DrawString(tmp, font, Brushes.Green, new PointF(tmp.Length * (-6),
            //        (HEIGHT / -2) + 25));
            //    //旋转45度
            //    graphics.RotateTransform(45);
            //    sp += 20;
            //}

            ////重置绘图平面的坐标变换
            //graphics.ResetTransform();
            //graphics.TranslateTransform(WIDTH / 2, HEIGHT / 2);

            ////绘制指针在30公里/秒的情形
            //float angle = 30 * 2.25f + 225;
            //graphics.RotateTransform((float)angle);

            //Pen pen = new Pen(Color.Blue, 14);
            ////设置线帽
            //pen.EndCap = LineCap.ArrowAnchor;
            //pen.StartCap = LineCap.RoundAnchor;
            ////画指针
            //graphics.DrawLine(pen, new PointF(0, 0), new PointF(0, (-1) * (HEIGHT / 2.75F)));

            //graphics.ResetTransform();
            //graphics.TranslateTransform(WIDTH / 2, HEIGHT / 2);
            ////绘制中心点
            //graphics.FillEllipse(Brushes.Black, -6, -9, 14, 14);
            //graphics.FillEllipse(Brushes.Red, -7, -7, 14, 14);

            ////绘制速度极限标记
            //pen.Width = 4;
            //pen.Color = Color.Black;
            //pen.EndCap = LineCap.Round;
            //pen.StartCap = LineCap.Flat;
            //graphics.DrawLine(pen, new PointF(HEIGHT / 15.75F, HEIGHT / 3.95F),
            //    new PointF(HEIGHT / 10.75F, HEIGHT / 5.2F));
            //pen.Color = Color.Red;
            //graphics.DrawLine(pen, new PointF(HEIGHT / 15.75F, HEIGHT / 3.95F),
            //    new PointF(HEIGHT / 15.75F, HEIGHT / 4.6F));
        }

        private Point Trans(int r, double rad, int ox, int oy)
        {
            Point pt = new Point(0, 0);
            int xx = (int)(r * Math.Cos(rad));
            int yy = (int)(r * Math.Sin(rad));
            pt.Y = -yy;
            pt.X = xx;
            pt.X += ox;
            pt.Y += oy;
            return pt;

        }

        public void Dispose()
        {
            throw new NotImplementedException();
        }
    }
}
